import 'dart:convert';

import 'package:get/get.dart';

import '../../../../generated/AlphaResponse.pb.dart';
import '../../../../generated/GetRobotWifiList.pb.dart';
import '../../../models/WifiInfo.dart';
import '../../../robot_comm/cmd_const.dart';
import '../../../robot_comm/mqtt_comm_manager.dart';

class WifiSetting2Controller extends GetxController {
  var wifiInfos = RxList<WifiInfo>();

  @override
  void onInit() {
    super.onInit();
    // 获取wifi列表数据.
    getWifiList();

  }

  void getWifiList() {
    GetRobotWifiListRequest request = GetRobotWifiListRequest();
    request.status = 0;
    MqttCommManager().sendCmd(CmdConst.IM_GET_WIFI_LIST_TO_ROBOT_REQUEST, request, (AlphaResponse msg) {
      GetRobotWifiListResponse wifiList  = GetRobotWifiListResponse.fromBuffer(msg.bodyData);
      print('GetRobotWifiListResponse =========> ${wifiList}');
      Map wifiData = json.decode(wifiList.wifilist);
      List<dynamic> childItems = json.decode(wifiData['p']);
      childItems.forEach((item) {
        WifiInfo wifiInfo = WifiInfo.fromJson(item);

        wifiInfos.add(wifiInfo);
      });

    });
  }


  @override
  void onReady() {
    super.onReady();

  }

  @override
  void onClose() {
    super.onClose();
  }
}
